What are indexes in IndexedDB and how do you use them?
169
02-Jul-2025
Manish Sharma
02-Jul-2025In IndexedDB, an index is a special data structure that lets you quickly search and retrieve records by a property other than the primary key (keyPath).
What is an Index?
CREATE INDEXon a column.Example: When to Use Indexes
Suppose your object store is:
You can:
"id"as the primary key (keyPath)"email"for quick lookupHow to Create an Index
Indexes are created in the
onupgradeneededevent when the database schema is defined or upgraded."emailIndex"= name of the index"email"= path to the field being indexed{ unique: true }= prevents duplicate values for that indexHow to Use an Index (Query)
You can also use:
index.getAll(value)index.openCursor()for iterationindex.count()to count matchesExample: Get All Users by Country
Benefits of Using Indexes
.openCursor()with directionSummary
onupgradeneededusingcreateIndex(name, keyPath, options).store.index('indexName').get(...),getAll(), oropenCursor().